<template>
  <el-dialog
    :visible.sync="SelectEmailDialog"
    :close-on-click-modal="false"
    width="45%"
    :before-close="handleClose"
    :modal-append-to-body="false"
  >
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="120px">
      <el-form-item label="请选择导出邮箱" prop="email">
        <el-select v-model="formData.email" class="form-input-large" placeholder="请选择导出邮箱">
          <el-option
            v-for="item in emailList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="export-tip">*导出全部数据耗时长且导出过程中不可取消，请谨慎操作</div>
    <div class="dialog-footer" slot="footer">
      <el-button type="primary" @click="handleConfirm">确定</el-button>
      <el-button type="info" plain @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'selectEmail',
  components: {},
  mixins: [],
  props: {
    value: {
      type: Boolean,
      default: false
    },
    type: {
      type: Number,
      default: 0
    }
  },
  computed: {
    SelectEmailDialog: {
      get() {
        return this.value
      },
      set(value) {
        this.$emit('input', value)
      }
    }
  },
  data() {
    return {
      options: {
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      },
      formData: {
        email: ''
      },
      rules: {
        email: [{ required: true, message: '请选择导出邮箱', trigger: 'change' }]
      },
      emailList: []
    }
  },
  created() {
    this.handleGetEmail()
  },
  methods: {
    // 获取邮箱
    async handleGetEmail() {
      const params = { type: this.type }
      const res = await this.$http.system.message.exportByEmail({ params })
      console.log('邮箱', res)
      this.emailList = res.data.map(el => ({
        ...el,
        label: el,
        value: el
      }))
    },
    //确定
    handleConfirm() {
      this.$refs.elForm.validate(valid => {
        if (valid) {
          this.$emit('exportAll', this.formData.email)
          this.SelectEmailDialog = false
          this.formData.email = ''
        } else {
          this.$message.warning('有必填项未填请检查')
        }
      })
    },
    //关闭
    handleClose() {
      this.SelectEmailDialog = false
    }
  }
}
</script>

<style scoped lang="scss">
.export-tip {
  width: 100%;
  font-size: 14px;
  color: #fc4541;
  text-align: right;
}
.wrap {
  display: flex;
  align-items: center;
  .text {
    margin: 0 10px;
  }
}
</style>
